//
//  ViewController.swift
//  tablewithsearchexample
//
//  Created by Avenue Zhang on 2019/6/11.
//  Copyright © 2019 Avenue Zhang. All rights reserved.
//

import UIKit

class ViewController: UIViewController
,UITableViewDataSource
    ,UITableViewDelegate
    , UISearchResultsUpdating
    ,UISearchBarDelegate
{
 
    

    
    
    
    @IBOutlet weak var zstable: UITableView!
    
    var list = [String]()
    
    var searchcontroller = UISearchController()

    override func viewDidLoad() {
        super.viewDidLoad()
        zstable.delegate = self
        zstable.dataSource = self
        self.definesPresentationContext = true
        searchcontroller = ({
            let controller = UISearchController(searchResultsController: nil)
       controller.searchResultsUpdater = self
            
            controller.dimsBackgroundDuringPresentation = false
            controller.searchBar.placeholder = "情输入企业名称"
            controller.searchBar.showsCancelButton = false
            controller.searchBar.keyboardType = .asciiCapable
            controller.searchBar.scopeButtonTitles = ["企业名称","姓名","身份证号","注册编号"]
            controller.searchBar.delegate = self
            
            zstable.tableHeaderView = controller.searchBar
            
            
            
        return controller
        })()
       
      
    }

    
    
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
          return list.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! UITableViewCell
        cell.textLabel?.text = list[indexPath.row]
        
        return cell
        
    }
    
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    
    func updateSearchResults(for searchController: UISearchController) {
        
    //print(searchcontroller.searchBar.text!)
        let key = searchcontroller.searchBar.text!
        
   
        
        

        
        
    }
    
    
    func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
        print(searchBar.text!)
        
        let key = searchBar.text!
        
        list.removeAll()
        
        if key.count > 4 {
            
            for i in 1...10 {
                
                list.append("数据\(i)")
                
            }
            
            zstable.reloadData()
            
            
        }
     
     
    }
    
    
    func searchBar(_ searchBar: UISearchBar, selectedScopeButtonIndexDidChange selectedScope: Int) {
        print("范围是\(selectedScope)")
    }
   

}

